package com.amigo.online.provider.sync.util;

import java.util.concurrent.ExecutorService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.ConstantValue;
import com.amigo.online.provider.sync.handle.service.DataHandler;

/**
 * @version: V 1.0.0
 * @Description: （对类进行功能描述）
 * @author: zxy QQ:1363320658
 * @date: 2018年12月10日 下午2:46:39
 */
@Service
public class DataHandlerBrokerUtil {

    @Autowired
    @Qualifier("videoRedisDataHandler")
    DataHandler videoRedisDataHandler;

    @Autowired
    @Qualifier("videoMongoDataHandler")
    DataHandler videoMongoDataHandler;

    @Autowired
    @Qualifier("videoSolrDataHandler")
    DataHandler videoSolrDataHandler;

    @Autowired
    @Qualifier("userRedisDataHandler")
    DataHandler userRedisDataHandler;

    @Autowired
    @Qualifier("userMongoDataHandler")
    DataHandler userMongoDataHandler;

    @Autowired
    @Qualifier("userSolrDataHandler")
    DataHandler userSolrDataHandler;

    @Autowired
    @Qualifier("musicRedisDataHandler")
    DataHandler musicRedisDataHandler;

    @Autowired
    @Qualifier("musicSolrDataHandler")
    DataHandler musicSolrDataHandler;

    @Autowired
    @Qualifier("updateQueueThreadPool")
    ExecutorService updateExecutorService;

    @Autowired
    @Qualifier("insertQueueThreadPool")
    ExecutorService insertExecutorService;

    /**
     * 更新操作
     *
     * @param tableName
     * @param type
     * @param newData
     * @param oldData
     */
    public void processingDistribution(String tableName, String type, JSONObject newData, JSONObject oldData) {
        if (ConstantValue.VIDEO_TBL.equals(tableName)) {
            videoUpdateHandler(newData, oldData);
        } else if (ConstantValue.USER_TBL.equals(tableName)) {
            userUpdateHandler(newData, oldData);
        } else if (ConstantValue.MUSIC_TBL.equals(tableName)) {
            musicUpdateHandler(newData, oldData);
        }
    }

    /**
     * 插入,删除操作
     *
     * @param tableName
     * @param type
     * @param newData
     */
    public void processingDistribution(String tableName, String type, JSONObject newData) {
        if (ConstantValue.TBL_INSERT.equals(type)) {
            if (ConstantValue.VIDEO_TBL.equals(tableName)) {
                videoInsertHandler(newData);
            } else if (ConstantValue.USER_TBL.equals(tableName)) {
                userInsertHandler(newData);
            } else if (ConstantValue.MUSIC_TBL.equals(tableName)) {
                musicInsertHandler(newData);
            }
        } else if (ConstantValue.TBL_DELETE.equals(type)) {

        }
    }

    /**
     * 音乐更新操作
     *
     * @param newData
     * @param oldData
     */
    private void musicUpdateHandler(JSONObject newData, JSONObject oldData) {
		/*updateExecutorService.execute(() -> {
			userMongoDataHandler.updateHandle(newData, oldData);
		});*/

        updateExecutorService.execute(() -> {
            musicSolrDataHandler.updateHandle(newData, oldData);
        });

		/*updateExecutorService.execute(() -> {
			musicRedisDataHandler.updateHandle(newData, oldData);
		});*/

    }

    /**
     * 音乐插入操作
     *
     * @param newData
     */
    private void musicInsertHandler(JSONObject newData) {
		/*insertExecutorService.execute(() -> {
			videoMongoDataHandler.insertHandle(newData);
		});*/

        insertExecutorService.execute(() -> {
            musicSolrDataHandler.insertHandle(newData);
        });

		/*insertExecutorService.execute(() -> {
			videoRedisDataHandler.insertHandle(newData);
		});*/
    }

    /**
     * video 更新操作
     *
     * @param newData
     * @param oldData
     */
    private void videoUpdateHandler(JSONObject newData, JSONObject oldData) {
        updateExecutorService.execute(() -> {
            videoMongoDataHandler.updateHandle(newData, oldData);
        });

        updateExecutorService.execute(() -> {
            videoSolrDataHandler.updateHandle(newData, oldData);
        });

		/*updateExecutorService.execute(() -> {
			videoRedisDataHandler.updateHandle(newData, oldData);
		});*/
    }

    /**
     * @throws
     * @Title: videoInsertHandler
     * @Description: (video 插入数据)
     * @param: @param newData
     * @return: void
     */
    private void videoInsertHandler(JSONObject newData) {
        insertExecutorService.execute(() -> {
            videoMongoDataHandler.insertHandle(newData);
        });

        insertExecutorService.execute(() -> {
            videoSolrDataHandler.insertHandle(newData);
        });

	/*	insertExecutorService.execute(() -> {
			videoRedisDataHandler.insertHandle(newData);
		});
		 */
    }

    /**
     * @throws
     * @Title: userUpdateHandler
     * @Description: (user 更新操作)
     * @param: @param newData
     * @param: @param oldData
     * @return: void
     */
    private void userUpdateHandler(JSONObject newData, JSONObject oldData) {
		/*updateExecutorService.execute(() -> {
			userRedisDataHandler.updateHandle(newData, oldData);
		});*/

        updateExecutorService.execute(() -> {
            userSolrDataHandler.updateHandle(newData, oldData);
        });

        updateExecutorService.execute(() -> {
            userMongoDataHandler.updateHandle(newData, oldData);
        });

    }

    /**
     * @throws
     * @Title: userInsertHandler
     * @Description: (user 插入操作)
     * @param: @param newData
     * @return: void
     */
    private void userInsertHandler(JSONObject newData) {
	/*	insertExecutorService.execute(() -> {
			userRedisDataHandler.insertHandle(newData);
		});*/

        insertExecutorService.execute(() -> {
            userMongoDataHandler.insertHandle(newData);
        });

        insertExecutorService.execute(() -> {
            userSolrDataHandler.insertHandle(newData);
        });

    }
}
